﻿using System;
using System.Collections;

public class ShoppingCart
{
    public string gProduct_id { get; set; }
    public string gProduct_name { get; set; }
    public int gQuantity { get; set; }
    public double gPrice { get; set; }
    public double gSubTotal { get; set; }
    public string gImage { get; set; }
    public  ArrayList gCart { get; set; }
    public ShoppingCart()
    {
        gCart = new ArrayList();
    }
    ///
    public ShoppingCart(string _product_id, String _product_name, int _quantity, double _price, String _image)
    {
        gCart = new ArrayList();
        gProduct_id = _product_id;
        gProduct_name = _product_name;
        gQuantity = _quantity;
        gPrice = _price;
        gSubTotal = _price * _quantity;
        gImage = _image;
    }
    public  void addCart(string _product_id, String _product_name, int _quantity, double _price, String _image)
    {
        int QtyBuy;
        QtyBuy = getQty(_product_id);
        if (QtyBuy == 0)
            gCart.Add(new ShoppingCart(_product_id, _product_name, _quantity, _price, _image));
        else
            updateCart(_product_id, QtyBuy + _quantity);

    }
    public  int getQty(string _product_id)
    {
        int kq = 0;
        if (gCart != null)
        {
            foreach (ShoppingCart h in gCart)
            {
                if (h.gProduct_id.Equals(_product_id))
                {
                    kq = h.gQuantity;
                    break;
                }
            }
        }
        return kq;
    }
    public  void updateCart(string _product_id, int _quantity)
    {

        foreach (ShoppingCart h in gCart)
        {
            if (h.gProduct_id == _product_id)
            {
                h.gQuantity = _quantity;
                h.gSubTotal = h.gPrice * _quantity;
                break;
            }
        }
    }
    public void removeCart(string _product_id)
    {
        foreach (ShoppingCart h in gCart)
        {
            if (h.gProduct_id.Equals(_product_id))
            {
                gCart.Remove(h);
                break;
            }
        }

    }
    public double getTotal()
    {
        double t = 0;
        if (gCart == null)
        {
            return 0;
        }
        foreach (ShoppingCart Item in gCart)
        {
            t += Item.gSubTotal;
        }
        return t;

    }
    public double getTotalQuantity()
    {
        double t = 0;
        if (gCart == null)
        {
            return 0;
        }
        foreach (ShoppingCart Item in gCart)
        {
            t += Item.gQuantity;
        }
        return t;

    }
}

